package gentle.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import gentle.service.IArrearsCustomerService;
import org.springframework.transaction.annotation.Transactional;
import gentle.entity.SearchCustomerInfo;
import gentle.entity.CustomerInfo;
import java.util.HashMap;
import gentle.mapper.ArrearsCustomerDao;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ArrearsCustomerImpl implements IArrearsCustomerService {
    @Autowired
    private ArrearsCustomerDao dao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public List<CustomerInfo> selectPageListByQueryGetMap(Page<CustomerInfo> page, SearchCustomerInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtils.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtils.isNotEmpty(searchInfo.getStartArrivalTime())) {
            params.put("startArrivalTime", searchInfo.getStartArrivalTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getEndArrivalTime())) {
            params.put("endArrivalTime", searchInfo.getEndArrivalTime());
        }
        params.put("tableName", searchInfo.getAbbreviation()+"customer_info");
        params.put("tableName1",searchInfo.getAbbreviation()+"deal_info");
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPage(page, params);
    }

    public int getQueryPagesCount(Page<CustomerInfo> page, SearchCustomerInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtils.isNotEmpty(searchInfo.getReach())) {
            params.put("reach", searchInfo.getReach());
        }
        if (StringUtils.isNotEmpty(searchInfo.getStartArrivalTime())) {
            params.put("startArrivalTime", searchInfo.getStartArrivalTime());
        }
        if (StringUtils.isNotEmpty(searchInfo.getEndArrivalTime())) {
            params.put("endArrivalTime", searchInfo.getEndArrivalTime());
        }
        params.put("tableName", searchInfo.getAbbreviation()+"customer_info");
        params.put("tableName1",searchInfo.getAbbreviation()+"deal_info");
        return dao.getQueryPagesCount(page, params);
    }

    public HashMap sumarrearsCustomer(String reach, String startArrivalTime, String endArrivalTime, String abbreviation){
        Map<String, Object> params = new HashMap<String, Object>();
        if (StringUtils.isNotEmpty(reach)) {
            params.put("reach", reach);
        }
        if (StringUtils.isNotEmpty(startArrivalTime)) {
            params.put("startArrivalTime", startArrivalTime);
        }
        if (StringUtils.isNotEmpty(endArrivalTime)) {
            params.put("endArrivalTime", endArrivalTime);
        }
        params.put("customer_info", abbreviation+"customer_info");
        params.put("deal_info",abbreviation+"deal_info");
        Map<String, Object> result = dao.sumarrearsCustomer(params);
        HashMap map = new HashMap();
        if (result != null) {
            map.put("borrowAmount_sum", result.get("borrowAmount_sum"));
        } else {
            map.put("borrowAmount_sum", "0");
        }
        return map;
    }

}